// PasswdGen.cpp
#include <iostream>
using namespace std;

void Usage()
{
    cout << endl << "PasswdGen 0.1  -  davidxl.blogspot.com" << endl << endl;
    cout << "Usage: PasswdGen -s <size> -c <chars> -h" << endl;
    cout << "  -h : Show Help (This)" << endl;
    cout << "  -s : Number of characters in password (default 8)" << endl;
    cout << "  -c : characters of password (default a..zA..Z..0..9)" << endl << endl;
    cout << "Example: PasswdGen -s 4 -c 0123456789"<< endl;
}

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        Usage();
        return 0;
    }

    unsigned int size = 8,i,clen;
    char * c = 0;

    for (i=1; i<argc ;i++)
    {
        if (strlen(argv[i]) > 1 && argv[i][0] == '-')
        {
            switch (argv[i][1])
            {
                case 's':
                    size = atoi(argv[i+1]);
                    break;
                case 'c':
                    c = argv[i+1];
                    break;
                case 'h':
                    Usage();
                    return 0;
                default:
                    cout << "Comand '-" << argv[i][1] << "' invalid.";
                    //return 1;
            }
        }
    }

    if(c == 0) c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
    clen = strlen(c);

    char * passwd = new char [size+1];
    for(i=0; i<size; i++)
        passwd[i] = c[rand()%clen];
    passwd[i] = '\0';

    cout << passwd;
}
